package com.rsxy.service.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.enums.SqlLike;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.rsxy.service.pojo.RsClassifty;
import com.rsxy.service.pojo.RsImage;
import com.rsxy.service.pojo.RsProduct;
import com.rsxy.service.service.RsClassiftyService;
import com.rsxy.service.service.RsImageService;
import com.rsxy.service.service.RsProductService;
import com.rsxy.service.tools.ResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.query.Param;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.extension.plugins.pagination.*;
import javax.annotation.Resource;
import javax.websocket.server.PathParam;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@Slf4j
@Api(tags= "商品相关接口")
@RequestMapping("/product")
public class ProductController {

    @Autowired
    private RsProductService rsProductService;
    @Autowired
    private RsImageService rsImageService;
    @Autowired
    private RsClassiftyService rsClassiftyService;
    @ApiOperation(value = "根据传入的ProductId获取信息",notes = "测试数据")
    @ApiImplicitParams({@ApiImplicitParam(name="productId",defaultValue = "",required = true,dataType = "string",paramType = "path")
    })
    @RequestMapping(method = RequestMethod.GET,value = "/{productId}")
    public RsProduct etProductInfoById(@PathVariable("productId") String productId){
        log.info("--------------proID-----------"+productId);
        RsProduct product= rsProductService.getProductInfoById(productId);

        return product;
    }
    @ApiOperation(value = "根据传入的ProductId删除指定商品",notes = "测试数据")
    @ApiImplicitParams({@ApiImplicitParam(name="productId",defaultValue = "",required = true,dataType = "string",paramType = "path")
    })
    @RequestMapping(method = RequestMethod.DELETE,value = "/{productId}")
    public String deleteById(@PathVariable("productId") String productId){
        QueryWrapper<RsProduct> rsProductQueryWrapper=new QueryWrapper<>();
        rsProductQueryWrapper.eq("product_id",productId);
        int delete = rsProductService.getBaseMapper().delete(rsProductQueryWrapper);
        if (delete>0){
            Map<String, Object> map = new HashMap<>();
            map.put("product_id",productId);
            int i = rsImageService.getBaseMapper().deleteByMap(map);
        }
        return ResultUtil.success(200,"删除成功",null);
    }
    @ApiOperation(value = "根据传入的ProductType获取商品列表",notes = "测试数据")
    @ApiImplicitParams({@ApiImplicitParam(name="product_type",defaultValue = "0",required = true,dataType = "int",paramType = "path")
    })
    @RequestMapping(method = RequestMethod.GET,value = "/getProductList/{product_type}")
    public String getProductList(@PathVariable("product_type") Integer product_type){
        List<RsProduct> productList = rsProductService.getProductList(product_type);
        return ResultUtil.success(200,"获取成功",productList);
}
    @ApiOperation(value = "获取商品分页列表",notes = "测试数据")
   // @ApiImplicitParams({@ApiImplicitParam(name="product_type",defaultValue = "0",required = true,dataType = "int",paramType = "path")
    @RequestMapping(method = RequestMethod.GET,value = "/getPageList")
    public IPage<RsProduct> getPageList(Integer page,Integer limit,Integer product_status,Integer product_type,String product_id){
        QueryWrapper<RsProduct> wrapper = new QueryWrapper();
        if (product_status!=null){
            wrapper.eq("product_status",product_status);
        }
        if (product_type!=null && product_type >= 0){
            wrapper.eq("product_type",product_type);
        }
        if (product_id!=null && product_id != ""){
            wrapper.eq("product_id",product_id);
        }
        //wrapper.eq("product_status",1);
        //wrapper.select(RsProduct.class,i -> !i.getColumn().equals("rs_image_list"));
        Page<RsProduct> page2=new Page<>(page,limit);
        IPage<RsProduct> page1 = rsProductService.getBaseMapper().selectPage(page2, wrapper);
        return page1;
    }
   // @ApiOperation(value = "获取商品分页列表",notes = "测试数据")
 // @ApiImplicitParams({@ApiImplicitParam(name="product_type",defaultValue = "0",required = true,dataType = "int",paramType = "path")
    @ApiOperation(value = "获取商品分页列表",notes = "测试数据")
    @ApiImplicitParams({@ApiImplicitParam(name="product_type",defaultValue = "0",required = true,dataType = "int",paramType = "path")})
    @RequestMapping(method = RequestMethod.GET,value = "/getSearchList")
    public String getSearchList(String product_title){
        List<RsProduct> searchList = rsProductService.getSearchList("%" + product_title + "%");
        return ResultUtil.success(100,"获取成功",searchList);
    }
    @ApiOperation(value = "添加商品",notes = "测试数据")
    @RequestMapping(method = RequestMethod.POST,value = "/addProduct",produces = "application/json;charset=UTF-8")
    public String addProduct(@RequestBody JSONObject jsonParam){

        JSONObject rsProduct = jsonParam.getJSONObject("rsProduct");
        JSONArray jsonArray = jsonParam.getJSONObject("rsProduct").getJSONArray("rsImageList");
        List<RsImage> list = JSONObject.parseArray(jsonArray.toJSONString(), RsImage.class);
        RsProduct rsProduct1 = JSONObject.toJavaObject(rsProduct, RsProduct.class);

       if (rsProduct1!=null){
            int rs = rsProductService.getBaseMapper().insert(rsProduct1);
            log.info("----rs-----"+rs);
            if (rs>0){
                for (RsImage rsImage : list){
                    int insert = rsImageService.getBaseMapper().insert(rsImage);
                }
                return ResultUtil.success(200,"添加成功~",null);
            }
        }


        return ResultUtil.success(200,"添加失败~",null);
    }
    @ApiOperation(value = "获取商品类别",notes = "测试数据")
    @RequestMapping(method = RequestMethod.GET,value = "/getClassifty",produces = "application/json;charset=UTF-8")
    public String getClassifty(){
        List<RsClassifty> rsClassifties = rsClassiftyService.getBaseMapper().selectList(null);
        return ResultUtil.success(200,"",rsClassifties);
    }
    @ApiOperation(value = "获取个人所有订单",notes = "测试数据")
    @RequestMapping(method = RequestMethod.GET,value="/{userId}/v1")
    public IPage<RsProduct> getPageInfoList(@PathVariable("userId") String userId,Integer page, Integer limit){
        QueryWrapper<RsProduct> wrapper = new QueryWrapper();
        wrapper.eq("user_id",userId);
        Page<RsProduct> page2=new Page<>(page,limit);
        IPage<RsProduct> page1 = rsProductService.getBaseMapper().selectPage(page2, wrapper);
        return page1;
    }
}
